package com.wedo.mall.activity.user;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.EStack;
import com.e.library.utils.EViewUtils;
import com.e.library.widget.ETitleBar;
import com.wedo.mall.R;
import com.wedo.mall.WApp;
import com.wedo.mall.activity.payment.PayActivity;
import com.wedo.mall.adapter.GoodsListAdapter;
import com.wedo.mall.adapter.OrderDetailAdapter;
import com.wedo.mall.dialog.SimpleDialog;
import com.wedo.mall.entity.GoodsData;
import com.wedo.mall.entity.OrderData;
import com.wedo.mall.util.DateUtils;
import com.wedo.mall.widget.StarView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;

public class OrderDetailActivity extends EActivity {

    @BindView(R.id.rv_goodslist) RecyclerView rv_goodslist;
    @BindView(R.id.tv_order_sn) TextView tv_order_sn;
    @BindView(R.id.tv_order_time) TextView tv_order_time;
    @BindView(R.id.tv_orderstate) TextView tv_orderstate;
    @BindView(R.id.tv_consignee) TextView tv_consignee;
    @BindView(R.id.tv_telephone) TextView tv_telephone;
    @BindView(R.id.tv_address) TextView tv_address;
    @BindView(R.id.tv_express_type) TextView tv_express_type;
    @BindView(R.id.tv_agent_name) TextView tv_agent_name;
    @BindView(R.id.tv_distance) TextView tv_distance;
    @BindView(R.id.tv_total) TextView tv_total;
    @BindView(R.id.tv_giving_point) TextView tv_giving_point;
    @BindView(R.id.tv_message) TextView tv_message;
    @BindView(R.id.iv_logo) ImageView iv_logo;
    @BindView(R.id.sv_star) StarView sv_star;
    @BindView(R.id.title_bar) ETitleBar title_bar;

    @BindView(R.id.ll_bottom) LinearLayout ll_bottom;
    @BindView(R.id.rl_cancel) RelativeLayout rl_cancel;
    @BindView(R.id.rl_pay) RelativeLayout rl_pay;
    @BindView(R.id.rl_receipt) RelativeLayout rl_receipt;
    @BindView(R.id.tv_remark) TextView tv_remark;
    @BindString(R.string.user_agent_remark) String remark_format;

    public String order_sn = "";


    private SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

    private GoodsListAdapter goodsListAdapter;
    private OrderDetailAdapter orderDetailAdapter;

    public static final String EXTRA_ORDER_SN = "order_sn";
    private String order;
    private String total;

    public static void toDetail(EActivity activity,String order_sn){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ORDER_SN,order_sn);
        activity.toActivity(OrderDetailActivity.class,bundle);
    }

    public static void toDetail(EActivity activity,String order_sn,String status){
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_ORDER_SN,order_sn);
        activity.toActivity(OrderDetailActivity.class,bundle);
    }

    @Override
    protected int layout() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        order_sn = getIntent().getStringExtra(EXTRA_ORDER_SN);
        Map<String,String> params = WApp.lngLat();
        params.put(EXTRA_ORDER_SN,order_sn);
        loading();
        post(EApi.USER_ORDERDETAIL, params, new EObserver() {
            @Override
            public void successful(EResponse response) {

                OrderData orderData = response.object("orderData",OrderData.class);
                order = orderData.order_sn;
                total = orderData.total;

                tv_order_sn.setText(orderData.order_sn);
                tv_order_time.setText(DateUtils.convert(format,Integer.parseInt(orderData.add_time)));
                //（0.待支付，1.待发货，2.待收货，3.待评价，4.已完成，5.作废，6.退换货）
                String status = "";
                if(orderData.status.equals("0")){
                    status = getString(R.string.user_unpaid);
                    ll_bottom.setVisibility(View.VISIBLE);
                    rl_cancel.setVisibility(View.VISIBLE);
                    rl_pay.setVisibility(View.VISIBLE);
                    rl_receipt.setVisibility(View.GONE);
                }else if(orderData.status.equals("1")){
                    status = getString(R.string.user_unprocessed);
                    /**待发货，不允许取消*/
                    ll_bottom.setVisibility(View.GONE);
                    rl_cancel.setVisibility(View.GONE);
                    rl_pay.setVisibility(View.GONE);
                    rl_receipt.setVisibility(View.GONE);
                }else if(orderData.status.equals("2")){
                    status = getString(R.string.user_notreceived);
                    ll_bottom.setVisibility(View.VISIBLE);
                    rl_cancel.setVisibility(View.GONE);
                    rl_pay.setVisibility(View.GONE);
                    rl_receipt.setVisibility(View.VISIBLE);
                }else if(orderData.status.equals("3")){
                    status = getString(R.string.user_pendingevaluation);
                    ll_bottom.setVisibility(View.GONE);
                }else if(orderData.status.equals("4")){
                    status = getString(R.string.user_finished);
                    ll_bottom.setVisibility(View.GONE);
                }else if(orderData.status.equals("5")){
                    status = getString(R.string.user_nullify);
                    ll_bottom.setVisibility(View.GONE);
                }else if(orderData.status.equals("6")){
                    status = getString(R.string.user_returned);
                    ll_bottom.setVisibility(View.GONE);
                }
                tv_orderstate.setText(status);

                StringBuilder address = new StringBuilder();
                address.append(orderData.province).append(orderData.city).append(orderData.county).append(orderData.address);

                tv_consignee.setText(orderData.consignee);
                tv_telephone.setText(orderData.telephone);
                tv_address.setText(address.toString());
                tv_total.setText(orderData.total);
                tv_giving_point.setText(orderData.giving_point);

                tv_remark.setText(String.format(remark_format,orderData.remark));

                List<GoodsData> goodsDataList =  response.list("goodsData",GoodsData.class);
                GoodsData goodsData = goodsDataList.get(0);
                if(goodsData.express_type.equals("0")){
                    tv_express_type.setText(R.string.user_doortodoor);
                }else if(goodsData.express_type.equals("1")){
                    tv_express_type.setText(R.string.user_tostore);
                }
                tv_message.setText(goodsData.message);

                rv_goodslist.setHasFixedSize(true);
                EViewUtils.vertical(rv_goodslist);
                orderDetailAdapter = new OrderDetailAdapter(OrderDetailActivity.this,new ArrayList<GoodsData>(),orderData);
                rv_goodslist.setAdapter(orderDetailAdapter);
                orderDetailAdapter.refresh(goodsDataList);
                dismiss();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
                EStack.onBackPressed(OrderDetailActivity.this,MyOrderActivity.class.getName());
                defaultFinish();
            }
        });

        title_bar.setOnBackwarListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EStack.onBackPressed(OrderDetailActivity.this,MyOrderActivity.class.getName());
                defaultFinish();
            }
        });
    }

    /**
     * 删除订单
     */
    public void cancelTheOrder(){
        Map<String,String> params = new HashMap<>();
        params.put(EXTRA_ORDER_SN,order_sn);
        loading();
        post(EApi.USER_CANCELTHEORDER, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                defaultFinish();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    /**
     * 确认收货
     */
    public void orderReceived(){
        Map<String,String> params = new HashMap<>();
        params.put(EXTRA_ORDER_SN,order_sn);
        loading();
        post(EApi.USER_RECEIVED, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                defaultFinish();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    @OnClick(R.id.rl_pay)
    protected void onPayClick(){
        PayActivity.toPay(this,order,total);
    }

    @OnClick(R.id.rl_cancel)
    protected void onCancelClick(){
        SimpleDialog dialog = SimpleDialog.with(this);
        dialog.setQuestion(getResources().getString(R.string.order_cancel_order));
        dialog.listener = new SimpleDialog.OnEnsureListener() {
            @Override
            public void onEnsure() {
                cancelTheOrder();
            }
        };
        dialog.show();
    }

    @OnClick(R.id.rl_receipt)
    protected void onReceiptClick(){
        orderReceived();
    }

    @Override
    public void onBackPressed() {
        EStack.onBackPressed(this,MyOrderActivity.class.getName());
        super.onBackPressed();
    }
}
